import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppUrl, TIME_OUT} from "../../../../app/app.general";
import {HttpClient} from "@angular/common/http";
import {CommonService} from "../../../../app/common.service";
import {AppConfig} from "../../../../app/appConfig";

/**
 * Generated class for the FoodOrderPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-food-order',
  templateUrl: 'food-order.html',
})
export class FoodOrderPage {
  private basket: any;
  private payQrCode;
  private isAdd: boolean;
  private totalPrice = 0;
  private orderNo = '';
  private orderDate = '';

  private orderDetails = {
    orderDetails: [],
    noPrintDetails: [],
    orderNo: '',
    orderDate: '',
    remark: '',
    sumPrice: 0,
    tId: 0
  };

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private httpService: HttpClient,
              private cs: CommonService,
              private appConfig: AppConfig) {
    this.basket = this.navParams.get('basket');
    this.isAdd = this.navParams.get('isAdd');
    if (this.isAdd === true) {
      this.orderNo = this.navParams.get('orderNo');
      this.appConfig.presentLoading("努力加载中，请稍后");
      this.httpService.post(AppUrl.food.getOrderDetail, this.orderNo).timeout(TIME_OUT).subscribe(data => {
        this.appConfig.loader.dismiss();
        let temp: any = data;
        if (temp.code == 200) {
          this.orderDetails = temp.data;
          this.totalPrice = +this.orderDetails.sumPrice;
          this.orderNo = this.orderDetails.orderNo;
          this.orderDate = this.orderDetails.orderDate;
        }
      }, e => {
        this.appConfig.loader.dismiss();
        this.cs.httpError(e);
      });
    }
    else {
      let orderData = this.navParams.get('orderData');
      this.orderNo = orderData.orderNo;
      this.orderDate = orderData.orderDate;
      this.totalPrice = this.basket.allPrice;
    }
    this.payQrCode = AppUrl.image.getPayQrCode + '/?orderNo=' + this.orderNo;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MarketOrderPage');
  }

  pay() {
    this.httpService.post(AppUrl.aliPay.alipayH5, {
      out_trade_no: this.orderNo,
      subject: '点餐结账',
      total_amount: this.totalPrice + '',
      description: '点餐结账'
    }).subscribe(res => {
      let temp: any = res;
      document.getElementById('form-hidden').innerHTML = temp.data;
      document.forms[0].submit();
    }, e => {
      this.cs.httpError(e);
    });
  }
}
